{% extends "bootstrap5/admin/base.html" %}
{% block title %}导航网站 - 用户管理{% endblock %}

{% block content %}
    <h1 class="mb-4">用户管理</h1>

    <div class="row mb-4">
        <div class="col-md-6">
            <h2>现有用户</h2>
        </div>
        <div class="col-md-6 text-end">
            <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">添加用户</button>
        </div>
    </div>

    <div class="table-responsive">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>邮箱</th>
                <th>角色</th>
                <th>状态</th>
                <th>创建日期</th>
                <th>更新日期</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {% for user in users %}
                <tr>
                    <td>{{ user.id }}</td>
                    <td>{{ user.username }}</td>
                    <td>{{ user.email }}</td>
                    <td>
                        {% if user.role == 'admin' %}
                            <span class="badge bg-primary">管理员</span>
                        {% else %}
                            <span class="badge bg-secondary">普通用户</span>
                        {% endif %}
                    </td>
                    <td>
                        {% if not user.is_deleted %}
                            <span class="badge bg-success">正常</span>
                        {% else %}
                            <span class="badge bg-danger">禁用</span>
                        {% endif %}
                    </td>
                    <td>{{ user.created_at }}</td>
                    <td>{{ user.updated_at }}</td>
                    <td>
                        <a href="{{ url_for('admin.user_detail', user_id=user.id) }}"
                           class="btn btn-sm btn-info">查看</a>
                        <form method="post" action="{{ url_for('admin.delete_user', user_id=user.id) }}"
                              style="display:inline;" onsubmit="return confirm('确定要删除该用户吗？');">
                            <input type="hidden" name="_method" value="DELETE">
                            <button type="submit" class="btn btn-sm btn-danger">删除</button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
            </tbody>
        </table>
    </div>

    <!-- 添加用户模态框 -->
    <div class="modal fade" id="addUserModal" tabindex="-1">
        <div class="modal-dialog">
            <form class="modal-content" method="post" action="{{ url_for('admin.users_add') }}">
                <div class="modal-header">
                    <h5 class="modal-title">添加用户</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="username" class="form-label">用户名</label>
                        <input type="text" class="form-control" id="username" name="username" required>
                    </div>
                    <div class="mb-3">
                        <label for="email" class="form-label">邮箱</label>
                        <input type="email" class="form-control" id="email" name="email" required>
                    </div>
                    <div class="mb-3">
                        <label for="role" class="form-label">角色</label>
                        <select class="form-select" id="role" name="role">
                            <option value="user">普通用户</option>
                            <option value="admin">管理员</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="password" class="form-label">密码</label>
                        <input type="password" class="form-control" id="password" name="password" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
                    <button type="submit" class="btn btn-primary">添加用户</button>
                </div>
            </form>
        </div>
    </div>
{% endblock %}
